package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.service.PresentService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;
import com.skyolder.milkbee.service.dto.PresentDTO;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * REST controller for managing Present.
 */
@RestController
@RequestMapping("/api")
public class PresentResource {

    private final Logger log = LoggerFactory.getLogger(PresentResource.class);
        
    @Inject
    private PresentService presentService;

    /**
     * POST  /presents : Create a new present.
     *
     * @param presentDTO the presentDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new presentDTO, or with status 400 (Bad Request) if the present has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/presents")
    @Timed
    public ResponseEntity<PresentDTO> createPresent(@RequestBody PresentDTO presentDTO) throws URISyntaxException {
        log.debug("REST request to save Present : {}", presentDTO);
        if (presentDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("present", "idexists", "A new present cannot already have an ID")).body(null);
        }
        PresentDTO result = presentService.save(presentDTO);
        return ResponseEntity.created(new URI("/api/presents/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("present", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /presents : Updates an existing present.
     *
     * @param presentDTO the presentDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated presentDTO,
     * or with status 400 (Bad Request) if the presentDTO is not valid,
     * or with status 500 (Internal Server Error) if the presentDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/presents")
    @Timed
    public ResponseEntity<PresentDTO> updatePresent(@RequestBody PresentDTO presentDTO) throws URISyntaxException {
        log.debug("REST request to update Present : {}", presentDTO);
        if (presentDTO.getId() == null) {
            return createPresent(presentDTO);
        }
        PresentDTO result = presentService.save(presentDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("present", presentDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /presents : get all the presents.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of presents in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/presents")
    @Timed
    public ResponseEntity<List<PresentDTO>> getAllPresents(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Presents");
        Page<PresentDTO> page = presentService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/presents");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /presents/:id : get the "id" present.
     *
     * @param id the id of the presentDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the presentDTO, or with status 404 (Not Found)
     */
    @GetMapping("/presents/{id}")
    @Timed
    public ResponseEntity<PresentDTO> getPresent(@PathVariable Long id) {
        log.debug("REST request to get Present : {}", id);
        PresentDTO presentDTO = presentService.findOne(id);
        return Optional.ofNullable(presentDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /presents/:id : delete the "id" present.
     *
     * @param id the id of the presentDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/presents/{id}")
    @Timed
    public ResponseEntity<Void> deletePresent(@PathVariable Long id) {
        log.debug("REST request to delete Present : {}", id);
        presentService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("present", id.toString())).build();
    }

}
